package com.xzk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import com.xzk.bean.BootStrapTableExpress;
import com.xzk.bean.Express;
import com.xzk.bean.Message;
import com.xzk.bean.ResultData;
import com.xzk.exception.DuplicateNumberException;
import com.xzk.service.ExpressService;
import com.xzk.util.DateFormatUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExpressController
 * @Description 快递控制器
 * @Author 0715-YuHao
 * @Date 2020/10/6 12:29
 * @Version 1.0
 */
@Controller
@RequestMapping("/express")
public class ExpressController {

    @Resource(name = "expressService")
    private ExpressService service;

    @RequestMapping("/console.do")
    public @ResponseBody Message console() {
        Map<String, Integer> data = service.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        return msg;
    }

    @RequestMapping("/list.do")
    public @ResponseBody ResultData<BootStrapTableExpress> list(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取分页查询的第几页
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //2. 获取当前页要查询的数据量
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //3. 进行查询
        PageHelper.startPage(pageNumber, pageSize);
        List<Express> list = service.findAll();
        PageInfo<Express> info = new PageInfo<>(list);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        int i = 1;
        for (Express e : info.getList()) {
            String userPhone = e.getUserPhone();
            String showUserPhone = userPhone.substring(0, 3) + "****" + userPhone.substring(7, 11);
            String sysPhone = e.getSysPhone();
            String showSysPhone = sysPhone.substring(0, 3) + "****" + sysPhone.substring(7, 11);
            list2.add(new BootStrapTableExpress(
                    i++,
                    e.getNumber(),
                    e.getUsername(),
                    showUserPhone,
                    e.getCompany(),
                    e.getCode() == null ? "已取件" : e.getCode(),
                    DateFormatUtil.formatDate(e.getInTime()),
                    e.getOutTime() == null ? "未出库" : DateFormatUtil.formatDate(e.getOutTime()),
                    e.getStatus() == 0 ? "待取件" : "已取件",
                    showSysPhone
            ));
        }
        //4. 将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(info.getTotal());
        return data;
    }

    @RequestMapping("/insert.do")
    public @ResponseBody Message insert(HttpServletRequest request) {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        // 管理员界面添加快递，默认录入人手机号18888887888
//        boolean insert = service.insert(new Express(number, username, userPhone, company, UserUtil.getUserPhone(request.getSession())));
        Message message = null;
        try {
            boolean insert = service.insert(new Express(number, username, userPhone, company, "18888887888"));
            if (insert) {
                //录入成功
                message = new Message(0, "录入成功");
            }else {
                //录入失败
                message = new Message(-1, "录入失败");
            }
        } catch (DuplicateNumberException e) {
            message = new Message(-1, "单号已存在");
        }
        return message;
    }



    @RequestMapping("/find.do")
    public @ResponseBody Message find(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Express e = service.findByNumber(number);
        Message message = new Message();
        if (e == null) {
            message.setStatus(-1);
            message.setContent("单号不存在！");
        }else {
            message.setStatus(0);
            message.setContent("查询成功！");
            message.setData(e);
        }
        return message;
    }

    @RequestMapping("/update.do")
    public @ResponseBody Message update(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        e.setUsername(username);
        e.setUserPhone(userPhone);
        e.setStatus(status);
        Message message = null;
        try {
            boolean update = service.update(id, e);
            if (update) {
                message = new Message(0, "修改成功!");
            }else {
                message = new Message(-1, "修改失败!");
            }
        } catch (DuplicateNumberException duplicateNumberException) {
            message = new Message(-1, "单号已存在!");
        }
        return message;
    }

    @RequestMapping("/delete.do")
    public @ResponseBody Message delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean delete = service.delete(id);
        Message message = new Message();
        if (delete) {
            message.setStatus(0);
            message.setContent("删除成功");
        }else {
            message.setStatus(-1);
            message.setContent("删除失败");
        }
        return message;
    }

}
